<?php

// [ 位移法 | 五码计划 ]
// use app\common\lib\Plan;
// Plan::run($no,$item[,$falg = false]);

namespace app\common\lib;
use think\Db;

class Plan
{
    private static $_limitArr = [
        '09' => 30,
        '10' => 30,
        '11' => 30,
        '12' => 30,
        '13' => 30,
        '14' => 30,
        '15' => 30,
        '16' => 30,
        '17' => 30,
        '18' => 30,
        '19' => 30,
        '20' => 30,
        '21' => 30,
        '22' => 30,
        '23' => 30,
    ];

    public static function run($no, $item, $flag = false)
    {
        // 获取源数据
        // -----------------------------------------------------------------------------------------
        $results = self::_results($no);
        // -----------------------------------------------------------------------------------------

        // 解析源数据
        // -----------------------------------------------------------------------------------------
        $item    = self::_analy($results,$item);
        // -----------------------------------------------------------------------------------------

        // 持久化数据
        // -----------------------------------------------------------------------------------------
        $flag && self::_db($no,$item);
        // -----------------------------------------------------------------------------------------

        return $item;
    }

    // 获取源数据
    private static function _results($no)
    {
        $results = Db::name('loc')->where('no','<',$no)->limit(30)->order('no DESC')->select();
        // p($results);die;
        return $results;
    }

    // 解析源数据
    private static function _analy($results, $result)
    {
        $item = [
            'v1' => [
                'loc_1'  => 0,
                'loc_2'  => 0,
                'loc_3'  => 0,
                'loc_4'  => 0,
                'loc_5'  => 0,
                'loc_6'  => 0,
                'loc_7'  => 0,
                'loc_8'  => 0,
                'loc_9'  => 0,
                'loc_10' => 0,
            ],
            'v2' => [
                'loc_1'  => 0,
                'loc_2'  => 0,
                'loc_3'  => 0,
                'loc_4'  => 0,
                'loc_5'  => 0,
                'loc_6'  => 0,
                'loc_7'  => 0,
                'loc_8'  => 0,
                'loc_9'  => 0,
                'loc_10' => 0,
            ],
            'v3' => [
                'loc_1'  => 0,
                'loc_2'  => 0,
                'loc_3'  => 0,
                'loc_4'  => 0,
                'loc_5'  => 0,
                'loc_6'  => 0,
                'loc_7'  => 0,
                'loc_8'  => 0,
                'loc_9'  => 0,
                'loc_10' => 0,
            ],
            'v4' => [
                'loc_1'  => 0,
                'loc_2'  => 0,
                'loc_3'  => 0,
                'loc_4'  => 0,
                'loc_5'  => 0,
                'loc_6'  => 0,
                'loc_7'  => 0,
                'loc_8'  => 0,
                'loc_9'  => 0,
                'loc_10' => 0,
            ],
            'v5' => [
                'loc_1'  => 0,
                'loc_2'  => 0,
                'loc_3'  => 0,
                'loc_4'  => 0,
                'loc_5'  => 0,
                'loc_6'  => 0,
                'loc_7'  => 0,
                'loc_8'  => 0,
                'loc_9'  => 0,
                'loc_10' => 0,
            ],
        ];
        foreach ($results as $key => $val) {
            for ($i=1; $i <= 5; $i++) {
                $vx  = 'v' . $i;
                $loc = $vx . '_loc';
                $num = $val[$loc];
                $item[$vx]['loc_'.$num]++;
            }
        }
        // p($item);die;

        foreach ($item as $key => $val) {
            arsort($item[$key]);
        }
        // p($item);die;

        foreach ($item as $key => $val) {
            $item[$key] = array_keys($item[$key]);
        }
        // p($item);die;

        foreach ($item as $key => $val) {
            foreach ($val as $k => $v) {
                $item[$key][$k] = ltrim($v,'loc_');
            }
        }
        // p($item);die;

        foreach ($item as $key => $val) {
            foreach ($val as $k => $v) {
                $item[$key][$k] = intval($result[$v-1]);
            }
        }
        // p($item);die;

        // 手动调整
        // $item = self::_debug($item);
        // p($item);die;

        return $item;
    }

    // 手动调整
    // private static function _debug($item)
    // {
    //     return $item;
    // }

    // 持久化数据
    private static function _db($no, $item)
    {
        $res = Db::name('plan')->where('no',$no)->find();
        if ( is_null($res) ) {
            $data['no']          = $no;
            $data['v1_loc']      = 0;
            $data['v1_hash']     = implode(',',$item['v1']);
            $data['v2_loc']      = 0;
            $data['v2_hash']     = implode(',',$item['v2']);
            $data['v3_loc']      = 0;
            $data['v3_hash']     = implode(',',$item['v3']);
            $data['v4_loc']      = 0;
            $data['v4_hash']     = implode(',',$item['v4']);
            $data['v5_loc']      = 0;
            $data['v5_hash']     = implode(',',$item['v5']);
            $data['create_time'] = time() + 300;
            // p($data);die;
            Db::name('plan')->insert($data);
        }
    }
}
